<%-- 
    Document   : test1
    Created on : 2015-10-10, 13:50:58
    Author     : Administrator
--%>


<%@page import="org.apache.http.util.EntityUtils"%>
<%@page import="org.apache.http.HttpEntity"%>
<%@page import="org.apache.http.impl.client.HttpClients"%>
<%@page import="org.apache.http.client.methods.CloseableHttpResponse"%>
<%@page import="org.apache.http.client.entity.UrlEncodedFormEntity"%>
<%@page import="org.apache.http.impl.client.CloseableHttpClient"%>
<%@page import="org.apache.http.protocol.HTTP"%>
<%@page import="org.apache.http.client.methods.HttpPost"%>
<%@page import="org.apache.http.message.BasicNameValuePair"%>
<%@page import="org.apache.http.NameValuePair"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
    String mode = request.getParameter("mode");

    if (mode.equalsIgnoreCase("sendMsg")) {
        String msg = request.getParameter("message");
        sendMessage(msg, "base.tronsystem.com:19027", "294", "公司网站");
    }


%>

<%!
    public static void sendMessage(String message, String url, String groupId, String from) {

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("groupId", groupId));
        params.add(new BasicNameValuePair("contact", message));
        params.add(new BasicNameValuePair("from", from));
        httpPost("http://" + url + "/app/message/sendGroupMessage.data", params);

    }

    public static String httpPost(String url, List<NameValuePair> list) {
        String result = null;
        try {
            // (1) Post请求
            HttpPost post = new HttpPost(url);

//            List<NameValuePair> params = new ArrayList<NameValuePair>();  
//params.add(new BasicNameValuePair("name", "ahopedog"));  
//params.add(new BasicNameValuePair("work", "程序员"));  
//            
            post.setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8));
            CloseableHttpClient http = null;
            CloseableHttpResponse response = null;
            try {
                // (3) 发送请求
                http = HttpClients.createDefault();
                response = http.execute(post);

                if (response.getStatusLine().getStatusCode() == 200) {
                    HttpEntity entity = response.getEntity();
                    result = EntityUtils.toString(entity, HTTP.UTF_8);

                    System.out.println("-----------------postresult:" + result);
                    return result;
                }
            } finally {
                response.close();
                http.close();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
%>
